GBML manual / Language

Functions

Named functions, default parameters, and the function you can write in place for foreach, filter and keep.

Declaring one

/// @arg {real} amount    how much damage to deal
function apply_damage(amount) {
    hp -= amount;
    if (hp < 0) { hp = 0; }
}

A parameter can have a default, used whenever a call leaves it out:

function heal(amount = 5) {
    hp = min(hp + amount, max_hp);
}

heal();       // heals 5
heal(20);     // heals 20

A function's parameter types come from a type written after the name (function heal(amount: int)) or its own /// documentation comment when it has one, or - when you write neither - from the calls: whatever a call hands that parameter (a number, a variable, a sum, what another script gives back) tells GBML its type, as long as the calls agree. Its return type comes from a type written after the parameters (function heal(amount: int): real), a /// @returns tag, or what its return statements actually return. Variables, types and scope covers the @arg/@returns tags in full, and Writing a type out lines each tag up with its written form; the short version is the same rule as everywhere else in GBML - one type, decided once, and the compiler tells you exactly what to add if it cannot work it out on its own.

Where a function can live

A function is usually its own file under scripts/ - Scripts covers creating and calling one in full. The same function name(...) { ... } syntax also parses and type-checks when written directly inside an event, but only a script's own top-level declaration (the one matching its own file's name) is ever reachable by a call; written anywhere else, it checks in isolation but cannot currently be called by anyone.

A function can also be written in place, without a name, in the one spot that wants a function: as the script handed to list_foreach, list_filter, list_keep and their array and grid twins, like list_foreach(scores, function (n) { total += n; }). There it is written out right where it stands, its parameter takes the kind of the items, and it can read and change the variables around it (but only for the length of that one call: nothing keeps it). A tag written above the statement that holds the call belongs to the function, exactly as it does for a script.

A function written in place cannot be kept. It may not be stored in a variable (var double = function (n) { return n * 2; };), put in a list or a struct, or handed to anything but foreach, filter or keep. The console has nothing to keep it in, so this is refused where it is written:

A function written out like `function (n) { ... }` cannot be kept in the variable `double` or used
anywhere except as the function handed to foreach, filter or keep.

Write it straight into the call that uses it, or make it a real script (a file under scripts/ with a function of the same name) and call it by name - see Scripts. GBML's design for a piece of behavior that also carries its own data along with it is a constructor (Structs and constructors) - but building one does not compile yet, so it is not something to reach for today. See What is unavailable for exactly what that means and what to write instead.

Calling with with

A script runs as the object that called it, so a call inside with runs the script as each object the with visits: inside the block, self is the one being visited, and that is who the script runs as. with (obj_enemy) { shake(); } calls shake once per living obj_enemy, and each time x, y, speed, every draw_ call and every move_ call inside shake belong to that enemy. See Writing a script for everything a script can do as its caller, including using a variable only some kinds of object have, like hp: inside with (obj_enemy) { apply_damage(5); } the script is the one built for obj_enemy, so its hp is each enemy's own.

A parameter is only ever a parameter, even when it is named like one of the object's own variables: in function aim(x: real, y: real), x is the number you passed in, and the caller's own x is self.x.

Scripts versus events

An event (create.gbml, step.gbml, alarm_0.gbml, …) is not itself a function you call - it is a slot the engine calls for you, at the point in the frame that event name promises (Events, The order of a frame). A script (a .gbml file under scripts/, or a function declared inside one) is the opposite: it does nothing until something calls it, whether that is an event, another script, or a cutscene step. Reach for a script whenever the same handful of lines would otherwise be copied into two or more events. Scripts covers creating, calling and shipping one in full - what a script's own parameters can be, how a container argument really behaves once it is inside the function, and what actually ends up in the ROM.

See also: Structs and constructorsExpressions and statementsVariables, types and scopeWhat is unavailable, and whyScriptsWriting a type out, instead of in a comment